<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleExport()" v-auth="'report:box:srmU8BoxDataDp:view'">
          <Icon icon="ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ invbarcode: record.invbarcode })">
          {{ record.invbarcode }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsReportBoxSrmU8BoxDataDpList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, onMounted } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { srmU8BoxDataDpDelete, srmU8BoxDataDpListData } from '/@/api/report/box/srmU8BoxDataDp';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl2 } from '/@/utils/file/download';

  const { t } = useI18n('report.box.srmU8BoxDataDp');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('箱码数据信息管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 120,
    schemas: [
      {
        label: t('箱码创建日期'),
        field: 'ddate',
        component: 'MonthPicker',
        componentProps: {
          format: 'YYYY-MM',
          valueFormat: 'YYYY-MM',
        },
        required: true,
      },
      {
        label: t('单品码'),
        field: 'invbarcode',
        component: 'Input',
      },
      {
        label: t('箱码'),
        field: 'boxbarcode',
        component: 'Input',
      },
      {
        label: t('存货名称'),
        field: 'cinvname',
        component: 'Input',
      },
      {
        label: t('批次'),
        field: 'bachnum',
        component: 'Input',
      },
      {
        label: t('生产日期'),
        field: 'scdate',
        component: 'MonthPicker',
        componentProps: {
          format: 'YYYY-MM',
          //showTime: false,
        },
      },
      {
        label: t('出库单号'),
        field: 'ckcode',
        component: 'Input',
      },
      {
        label: t('发货单号'),
        field: 'cdlcode',
        component: 'Input',
      },
      {
        label: t('客户名称'),
        field: 'ccusname',
        component: 'Input',
      },
      {
        label: t('物流公司'),
        field: 'cexpressconame',
        component: 'Input',
      },
      {
        label: t('物流单号'),
        field: 'cebexpresscode',
        component: 'Input',
      },
      {
        label: t('发货罐数'),
        field: 'qty',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('单品码'),
      dataIndex: 'invbarcode',
      key: 'a.invbarcode',
      sorter: true,
      width: 230,
      align: 'left',
      //slot: 'firstColumn',
    },
    {
      title: t('箱码'),
      dataIndex: 'boxbarcode',
      key: 'a.boxbarcode',
      sorter: true,
      width: 180,
      align: 'left',
    },
    {
      title: t('存货名称'),
      dataIndex: 'cinvname',
      key: 'a.cinvname',
      sorter: true,
      width: 260,
      align: 'left',
    },
    {
      title: t('批次'),
      dataIndex: 'bachnum',
      key: 'a.bachnum',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('生产日期'),
      dataIndex: 'scdate',
      key: 'a.scdate',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('出库单号'),
      dataIndex: 'ckcode',
      key: 'a.ckcode',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('发货单号'),
      dataIndex: 'cdlcode',
      key: 'a.cdlcode',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('客户名称'),
      dataIndex: 'ccusname',
      key: 'a.ccusname',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('物流公司'),
      dataIndex: 'cexpressconame',
      key: 'a.cexpressconame',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('物流单号'),
      dataIndex: 'cebexpresscode',
      key: 'a.cebexpresscode',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('发货罐数'),
      dataIndex: 'qty',
      key: 'a.qty',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('箱码创建日期'),
      dataIndex: 'ddate',
      key: 'a.ddate',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑箱码数据信息'),
        onClick: handleForm.bind(this, { invbarcode: record.invbarcode }),
        auth: 'report:box:srmU8BoxDataDp:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除箱码数据信息'),
        popConfirm: {
          title: t('是否确认删除箱码数据信息'),
          confirm: handleDelete.bind(this, { invbarcode: record.invbarcode }),
        },
        auth: 'report:box:srmU8BoxDataDp:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: srmU8BoxDataDpListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    //actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  onMounted(async () => {
    handleProjecte();
  });

  async function handleProjecte() {
    await getForm().setFieldsValue({
      ddate: new Date(),
    });
    reload();
    await getForm().setFieldsValue({
      ddate: new Date(),
    });
  }

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await srmU8BoxDataDpDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  // 导出
  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl2({
      url: ctxAdminPath + '/report/box/srmU8BoxDataDp/export',
      target: '_self',
      params: getForm().getFieldsValue(),
    });
  }

  function handleSuccess() {
    reload();
  }
</script>
